package com.ampaiva.view;

import android.graphics.Point;
import android.graphics.Rect;

public class Geometry {

	public static Point getRigthMiddlePoint(Rect rect) {
		return new Point(rect.right, rect.bottom - (rect.bottom - rect.top) / 2);
	}

	public static Point getLeftMiddlePoint(Rect rect) {
		return new Point(rect.left, rect.bottom - (rect.bottom - rect.top) / 2);
	}

	public static int getMiddleX(Rect rect) {
		return rect.right - (rect.right - rect.left) / 2;
	}

	public static int getMiddleY(Rect rect) {
		return rect.bottom - (rect.bottom - rect.top) / 2;
	}

	public static Point[] getMiddlePoints(Rect rect) {
		Point pLeft = new Point(rect.left, getMiddleY(rect));
		Point pTop = new Point(getMiddleX(rect), rect.top);
		Point pRight = new Point(rect.right, getMiddleY(rect));
		Point pBottom = new Point(getMiddleX(rect), rect.bottom);
		return new Point[] { pLeft, pTop, pRight, pBottom };
	}

	private static int getDistX(Point point1, Point point2) {
		return Math.abs(point2.x - point1.x);
	}

	private static int getDistY(Point point1, Point point2) {
		return Math.abs(point2.y - point1.y);
	}

	private static double getDist(Point point1, Point point2) {
		int distX = getDistX(point1, point2);
		int distY = getDistY(point1, point2);
		return Math.sqrt(distX * distX + distY * distY);
	}

	public static Point[] getNearestMiddlePoints(Rect rect1, Rect rect2) {
		Point[] mp1 = getMiddlePoints(rect1);
		Point[] mp2 = getMiddlePoints(rect2);

		Point[] points = new Point[2];
		double minDist = Double.MAX_VALUE;
		for (int i = 0; i < mp1.length; i++)
			for (int j = 0; j < mp2.length; j++) {
				double dist = getDist(mp1[i], mp2[j]);
				if (dist < minDist) {
					minDist = dist;
					points[0] = mp1[i];
					points[1] = mp2[j];
				}
			}
		return points;
	}
}
